
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
import java.sql.*;


public class get_Thermostat_Info extends HttpServlet{
	
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
	  int current_temp=-1;
	  int op_mode=-1;
	  int target_temp=-1;
	  int fan_mode=-1;
	  String user="";
	  String temp="";
	  HttpSession session = request.getSession(true);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Enumeration paramNames = request.getParameterNames();
    while(paramNames.hasMoreElements()) {
      String paramName = (String)paramNames.nextElement();
	  String paramValue=request.getParameter(paramName);
	  String a="66";
	  System.out.println(paramValue);
		if(paramName.equals("user"))
		user=paramValue;
		else if(paramName.equals("temp"))
		temp=paramValue;
      }
	  System.out.println("TEMPERAUTRE SENT FROM CLIENT:"+temp);
	Connection conn=null;
	Statement st=null;

	String url = "jdbc:oracle:thin:@localhost:1521";
	String dbName = ":orcl";
	String driver = "oracle.jdbc.driver.OracleDriver";
	String userName = "scott"; 
	String password = "tiger";
	
	try{
		Class.forName(driver).newInstance();
		}catch(Exception e){
		e.printStackTrace();
		}	
		try{
			conn = DriverManager.getConnection(url+dbName,userName,password);
			st=conn.createStatement();
		}catch(SQLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(!user.equals(""))
		{
			//System.out.println(current_temp+" "+op_mode+" "+fan_mode+" "+user);
			//String query="select * from Thermostat_Current_Information where username='"+user+"'";
			try{
			ResultSet rs=null;
			rs=st.executeQuery("select * from Thermostat_Current_Information where username='"+user+"'");
				while(rs.next())
				{
					//out.println("Hello");
					out.println(rs.getString(4));
					//out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
				}
			}catch(Exception e){e.printStackTrace();}
			try{
			st.executeUpdate("insert into Log_Temperature values(to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS'),"+Double.parseDouble(temp)+")");
			}
			catch(Exception e)
			{
					System.out.println("Logging Error");
			}
			try{
			if(conn!=null)
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
			//response.sendRedirect("welcome.jsp");
		}
		
			
		}
 

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}
